package net.lainiao.dicom.common.storage;

import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;

import java.io.File;

public class StorageLocalUtil implements StorageUtil {
    private String localPath;

    public StorageLocalUtil(String localPath) {
        this.localPath = localPath;
    }

    @Override
    public void open() throws Exception {
        File file = new File(localPath);
        if (!file.exists()) {
            throw new Exception("LocalPath Is Not Exists! Value:" + localPath);
        }
    }

    @Override
    public void putObject(String objectName, File file) throws Exception {
        String fileSavePath = localPath +  File.separator + objectName;
        File saveFile = new File(fileSavePath);
        File parentFile = saveFile.getParentFile();
        if (!parentFile.exists()) {
            boolean bool = parentFile.mkdirs();
            if (!bool) {
                throw new Exception("本地磁盘创建目录失败:(" + parentFile.getAbsolutePath() + ")");
            }
        }
        FileCopyUtils.copy(file, saveFile);
    }

    public void putObject(String objectName, byte[] bytes) throws Exception {
        String fileSavePath = localPath +  File.separator + objectName;
        File saveFile = new File(fileSavePath);
        File parentFile = saveFile.getParentFile();
        if (!parentFile.exists()) {
            boolean bool = parentFile.mkdirs();
            if (!bool) {
                throw new Exception("本地磁盘创建目录失败:(" + parentFile.getAbsolutePath() + ")");
            }
        }
        FileCopyUtils.copy(bytes, saveFile);
    }

    public void putObject(String objectName, String sourceFilePath) throws Exception {
        String fileSavePath = localPath +  File.separator + objectName;
        File saveFile = new File(fileSavePath);
        File parentFile = saveFile.getParentFile();
        if (!parentFile.exists()) {
            boolean bool = parentFile.mkdirs();
            if (!bool) {
                throw new Exception("本地磁盘创建目录失败:(" + parentFile.getAbsolutePath() + ")");
            }
        }
        File sourceFile=new File(sourceFilePath);
        boolean bool=sourceFile.renameTo(saveFile);
        if(!bool){
            FileCopyUtils.copy(sourceFile, saveFile);
        }
    }

    @Override
    public void removeObject(String objectName) {
        String fileSavePath = localPath +  File.separator + objectName;
        File saveFile = new File(fileSavePath);
        FileSystemUtils.deleteRecursively(saveFile);
    }

    @Override
    public void getObject(String objectName, File targetFile) throws Exception {
        String fileSavePath = localPath + File.separator + objectName;
        File saveFile = new File(fileSavePath);
        if (saveFile.exists()) {
            FileCopyUtils.copy(saveFile,targetFile);
        }
        else {
            throw new Exception("获取本地文件对象失败，对象不存在! ObjectName=" + objectName);
        }
    }

    @Override
    public boolean doesObjectExist(String objectName) {
        String fileSavePath = localPath +  File.separator + objectName;
        File saveFile = new File(fileSavePath);
        return saveFile.exists();
    }

    @Override
    public void close() {

    }
}
